<template>
  <div class="edit-production">
    <div class="top">{{titleVal}}</div>
    <div class="content mt">
      <el-form
        ref="ruleForm"
        :rules="rules"
        :model="ruleForm"
        label-width="80px"
        style="width: 500px"
      >
        <el-form-item label="名称:">
          <el-input
            type="text"
            v-model="ruleForm.title"
            maxlength="46"
            show-word-limit
            size="medium"
          />
        </el-form-item>
        <el-form-item label="封面图:">
          <el-upload
            action="http://ddsc.izhixiu.cn/api/common/upload"
            list-type="picture-card"
            :file-list="coverFileList"
            :on-preview="handlePictureCardPreview"
            :limit="1"
            :on-success="hanldeCoverSuccess"
            name="photo"
            :on-remove="handleRemove"
          >
            <i class="el-icon-plus" />
          </el-upload>
          <p style="color:#787878;font-size:12px;">建议尺寸：750*420px</p>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt />
          </el-dialog>
        </el-form-item>
        <el-form-item label="简介:">
          <el-input maxlength="120" show-word-limit v-model="ruleForm.content" type="textarea" />
        </el-form-item>
        <el-form-item label="上传图片">
          <div class="tab">
            <div class="card" :class="{'active': activeName == 1}">
              <!-- <el-radio v-model="radio" :label="1" @change="handleChange(1)">跳转详情</el-radio> -->
              <el-radio v-model="radio" :label="1" @change="handleChange(1)">添加图片</el-radio>
            </div>
            <div class="card" :class="{'active': activeName == 2}">
              <el-radio v-model="radio" :label="2" @change="handleChange(2)">跳转链接</el-radio>
            </div>
          </div>
          <div class="tab-content">
            <div class="content">
              <div v-if="activeName == 1" class="tinymce-bo">
                <!-- <Tinymce ref="tinymce" v-model="ruleForm.detail" /> -->
                <div style="display:flex;">
                  <el-form-item prop="banners" label>
                    <el-upload
                      action="http://ddsc.izhixiu.cn/api/common/upload"
                      name="photo"
                      :multiple="true"
                      :limit="13"
                      :file-list="carouselFileList"
                      list-type="picture-card"
                      :before-upload="handleCoverBefore"
                      :on-preview="handleCarouselPreview"
                      :on-remove="handleCarouselRemove"
                      :on-success="hanldeCarouselSuccess"
                    >
                      <i class="el-icon-plus" />
                    </el-upload>
                    <el-dialog :visible.sync="carouselDialogVisible">
                      <img width="100%" :src="carouselDialogImageUrl" alt />
                    </el-dialog>
                  </el-form-item>
                </div>
              </div>
              <div v-if="activeName == 2" class="link-box">
                <span>输入连接:</span>
                <el-input
                  v-model="ruleForm.link"
                  size="medium"
                  style="width: 300px;margin-left: 20px"
                />
              </div>
            </div>
          </div>
        </el-form-item>
      </el-form>
    </div>
    <div class="save">
      <button class="submit" @click="save('ruleForm')">保存</button>
    </div>
  </div>
</template>
<script>
import Tinymce from "@/components/Tinymce";
import { editDesignerWorks } from "@/api/http/designer";
const ERR_OK = 200;
export default {
  components: {
    Tinymce
  },
  data() {
    return {
      titleVal: "添加作品",
      coverFileList: [],
      carouselFileList: [],
      carouselDialogVisible: false,
      carouselDialogImageUrl: "",
      designerId: 0,
      id: 0,
      link: "",
      banners: [],
      ruleForm: {
        title: "",
        detail: "",
        link: "",
        content: "",
        banners: []
      },
      dialogVisible: false,
      dialogImageUrl: "",
      activeName: 1,
      radio: 1,
      rules: {
        title: [
          { required: true, message: "请填写名称", trigger: "blur" },
          { min: 4, max: 46, message: "长度在 4 到 46 个字符", trigger: "blur" }
        ],
        detail: [{ required: true, message: "请填写详情", trigger: "blur" }]
      }
    };
  },
  created() {
    if (this.$route.query.designerId) {
      this.designerId = this.$route.query.designerId;
    }
    if (this.$route.query.id) {
      this.id = this.$route.query.id;
      this.titleVal = "编辑作品";
      this.ruleForm = JSON.parse(window.localStorage.getItem("designerMes"));
      if (this.ruleForm.status) {
        this.activeName = this.ruleForm.status;
      }
      if (this.ruleForm.status) {
        this.radio = this.ruleForm.status;
      }
      this.carouselFileList = [];
      if (this.ruleForm.images) {
        this.ruleForm.images.map(res => {
          this.carouselFileList.push({
            name: "file",
            url: res.image
          });
        });
      }
      this.coverFileList.push({
        name: "file",
        url: this.ruleForm.cover
      });
    }
  },
  methods: {
    handleCoverBefore(file) {
      console.log(file, file);
    },
    handleCarouselPreview(file) {
      this.carouselDialogImageUrl = file.url;
      this.carouselDialogVisible = true;
    },
    handleCarouselRemove(file) {
      const fIndex = this.form.banners.findIndex(item => {
        return item === file.url;
      });
      if (fIndex >= 0) {
        this.ruleForm.banners.splice(fIndex, 1);
      }
      this.carouselDialogImageUrl = "";
    },
    hanldeCarouselSuccess(response, file, fileList) {
      if (response.code === 200) {
        if (!this.ruleForm.banners) {
          this.ruleForm.banners = [];
        }
        this.ruleForm.banners.push(response.data.photo);
      }
    },
    // 提交表单
    save(ruleForm) {
      if (!this.ruleForm.title) {
        this.$message("请填写作品名称");
        return;
      }
      if (!this.ruleForm.cover) {
        this.$message("请上传封面图");
        return;
      }
      if (!this.ruleForm.title) {
        this.$message("请填写作品简介");
        return;
      }
      if (!this.ruleForm.detail && !this.ruleForm.link) {
        this.$message("请填写作品详情或者设置跳转链接");
        return;
      }
      console.log(this.ruleForm, "---ruleForm--888");
      this.$refs[ruleForm].validate(valid => {
        if (valid) {
          console.log("校验通过");
          var params = {
            designer_id: parseInt(this.designerId),
            cover: this.ruleForm.cover,
            content: this.ruleForm.content,
            title: this.ruleForm.title,
            link: this.ruleForm.link,
            detail: this.ruleForm.detail,
            status: this.activeName,
            images: this.ruleForm.banners
          };
          if (this.id) {
            params.id = this.id;
          }
          editDesignerWorks(params).then(res => {
            console.log(res, "---r-es");
            if (res.code == 200) {
              if (this.id) {
                this.$message.success("编辑成功");
              } else {
                this.$message.success("添加成功");
              }
              this.$router.go(-1);
            }
          });
        } else {
          console.log("未通过");
        }
      });
    },
    hanldeCoverSuccess(response, file, fileList) {
      if (response.code === 200) {
        this.ruleForm.cover = response.data.photo;
      }
    },
    handlePictureCardPreview(file) {
      // this.dialogImageUrl = file.url;
    },
    handleRemove(file, fileList) {
      this.fileList = [];
      this.ruleForm.cover = "";
    },
    handleClick() {},
    handleChange(index) {
      this.radio = index;
      this.activeName = index;
    }
  }
};
</script>
<style lang="scss" scoped>
.top {
  line-height: 26px;
  color: rgba(68, 68, 68, 1);
  font-size: 18px;
  text-align: left;
  font-family: Roboto;
  font-weight: bold;
}
.tab {
  display: flex;
  align-items: center;
  .card {
    width: 120px;
    line-height: 41px;
    background-color: #fff;
    text-align: center;
    border: 1px solid rgba(255, 255, 255, 0);
    &.active {
      background-color: rgba(242, 242, 242, 1);
    }
  }
}
.tab-content {
  width: 993px;
  min-height: 143px;
  background-color: rgba(242, 242, 242, 1);
  text-align: center;
  border: 10px solid #f2f2f2;
  .link-box {
    display: flex;
    align-items: center;
    white-space: nowrap;
    margin-top: 30px;
  }
}
.save {
  .submit {
    display: block;
    margin: 0 auto;
    width: 200px;
    line-height: 48px;
    border-radius: 6px;
    background-color: rgba(43, 51, 62, 1);
    text-align: center;
    font-size: 16px;
    color: #fff;
    border: none;
    outline: none;
  }
}
.mt {
  margin-top: 30px;
}
/deep/ .el-upload-list__item img {
  object-fit: contain;
}
</style>
